/*
 * The MIT License 
 *
 * Copyright (c) 2010 pf-miles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package me.wenyue.jclassview.handler;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.wenyue.jclassview.binview.BinView;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

/**
 * renders the application's main page
 * 
 * @author pf-miles 2010-11-10 下午07:36:06
 * 
 */
public class ViewHandler extends AbstractHandler {
    private static final Properties p = new Properties();
    static {
        try {
            p.load(new FileReader("conf/velocity.properties"));
            Velocity.init(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void handle(String target, Request baseRequest, HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {
        byte[] data = resolveData(request);
        BinView binView = buildBinView(data);
        Context context = new VelocityContext();
        context.put("binView", binView);
        mergeTemplate(response, context);
    }

    private BinView buildBinView(byte[] data) {
        // TODO dummy implementation
        return new BinView(data, "blahblahblah");
    }

    private byte[] resolveData(HttpServletRequest request) {
        // TODO dummy implementation
        byte[] ret = new byte[129];
        for (int i = 9; i < 9 + 129; i++) {
            ret[i - 9] = (byte) i;
        }
        return ret;
    }

    private void mergeTemplate(HttpServletResponse response, Context context) throws IOException {
        Template main = null;
        try {
            main = Velocity.getTemplate("main.vm", "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Writer w = new BufferedWriter(response.getWriter());
        main.merge(context, w);
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        w.flush();
    }

}
